package com.legleg.servlet;

import com.legleg.data.Album;
import com.legleg.data.json.AlbumBean;
import com.legleg.handler.AlbumHandler;
import com.legleg.util.BaseUtil;
import com.legleg.util.RequestUtil;
import net.sf.json.JSONArray;
import org.apache.velocity.Template;
import org.apache.velocity.context.Context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 12-12-26
 * Time: 下午5:11
 * To change this template use File | Settings | File Templates.
 */
public class Index extends BaseServlet {
    @Override
    protected Template execute(HttpServletRequest request, HttpServletResponse response, Context ctx) {
        int page = RequestUtil.getInt(request,"p",1);
        int size = RequestUtil.getInt(request,"s",40);
        List<Album> albums = AlbumHandler.getInstance().getRefresh(page, size);
        if(albums!=null && albums.size()>0){
            ArrayList<AlbumBean> list = new ArrayList<AlbumBean>();
            for(Album album:albums){
                list.add(new AlbumBean(album.getId(),album.getTitle(), BaseUtil.getAlbumThumb(album.getId()),album.getTags(),album.getPicnum()));
            }
            JSONArray jarr = JSONArray.fromObject(list);
            if(jarr.size()>0)
                ctx.put("json", jarr.toString());
        }

        return getTemplate("index.vm");
    }
}
